package com.lanou.activity.controller;

import com.github.pagehelper.PageInfo;
import com.lanou.activity.Service.SLGFService;
import com.lanou.activity.utils.ResultUtil;
import com.lanou.activity.utils.ToolUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 买赠的Controller层
 */
@Controller
@RequestMapping("/slgf")
public class SLGFController {

    @Resource
    private SLGFService slgfService;


    /**
     * @description 跳转到添加买赠页面  addSLGF.ftl
     * @author Lonely
     * @date 17/9/22
     * @return
     * @param
     */
    @RequestMapping("findSLGF")
    public ModelAndView findSLGF() {
        ModelAndView modelAndView = new ModelAndView();

//        查询促销条件表的促销条件编号conNo和促销条件名称conName
        List<Map<String,Object>> params = slgfService.findSaleCdnInfo();

//        调用商品分类的接口 获取 商品分类的编号prdNo 和 商品分分类的名字prdName
//        Map<String,Object> map = (Map<String, Object>) ToolUtil.callUrl("http://10.90.86.212:8888/prd/promanngerN");
        List<Map<String,Object>> map = slgfService.selectPrdInfo();
        modelAndView.addObject("params",params);
        modelAndView.addObject("map",map);
        modelAndView.setViewName("addSLGF");
        return modelAndView;
    }

    /**
     * @description 调用商品明细接口，根据商品分类名prd_name获取商品明细信息
     * @author Lonely
     * @date 17/9/23
     * @return
     * @param params
     */
    @RequestMapping("getPrdDetailInfo")
    @ResponseBody
    public List<Map<String,Object>> getPrdDetailInfo(@RequestParam Map<String,Object> params) {

//        调用商品明细接口 根据 商品分类名称 prdName 查询 商品明细编号prd_dtl_id和名字prd_dtl_name
        List<Map<String,Object>> proList = (List<Map<String, Object>>) ToolUtil.callUrl("http://10.90.86.249:8888/prd/getAll2",params);
        params = ResultUtil.success(proList);
        System.out.println("proList = " + proList);
        return proList;

    }

    /**
     * @description 添加买赠信息
     * @author Lonely
     * @date 17/9/23
     * @return
     * @param params 从前端传递的买赠信息的数据
     */
    @RequestMapping("addSLGFInfo")
    @ResponseBody
    public Map<String,Object> addSLGFInfo(@RequestParam Map<String,Object> params) {
//        使用账号生成器自动生成买赠账号
        String gNo = ToolUtil.AccountGenerator("g");
        params.put("gNo",gNo);

        int result = slgfService.addSLGFInfo(params);
        params.put("result","success");
        return params;
    }

    /**
     * @description 跳转到买赠信息管理页面 slgfManage.ftl 以及 查询买赠信息表信息
     * @author Lonely
     * @date 17/9/25
     * @return
     * @param
     */
    @RequestMapping("selectSLGFInfo")
    public ModelAndView selectSLGFInfo(@RequestParam Map<String,Object> params) {

        System.out.println("params = "+params);

        ModelAndView modelAndView = new ModelAndView();
        PageInfo<Map<String, Object>> pageInfo = slgfService.selectSLGFInfo(params);

//        查询促销条件表的促销条件编号conNo和促销条件名称conName
        List<Map<String,Object>> conList = slgfService.findSaleCdnInfo();
//        调用商品分类的接口 获取 商品分类的编号prdNo 和 商品分分类的名字prdName
        List<Map<String,Object>> map = slgfService.selectPrdInfo();

        System.out.println("pageInfo.getList() = " + pageInfo.getList().toString());
        System.out.println("page = " + pageInfo);


        modelAndView.addObject("page",pageInfo);
        modelAndView.addObject("list",pageInfo.getList());
        modelAndView.addObject("params",params);
        modelAndView.addObject("conList",conList);
        modelAndView.addObject("map",map);
        modelAndView.setViewName("slgfManage");
        return modelAndView;
    }


    /**
     * @description 根据买赠id查询买赠的详细信息  回显数据
     * @author Lonely
     * @date 17/9/26
     * @return
     * @param
     */
    @RequestMapping("selectSLGFDetailInfo")
    @ResponseBody
    public ModelAndView selectSLGFDetailInfo(@RequestParam("id") int id) {
        ModelAndView modelAndView = new ModelAndView();
        Map<String,Object> param = new HashMap<>();

//        根据id查询买赠信息
        Map<String,Object> params = slgfService.selectSLGFDetailInfo(id);

        param.put("prd_dtl_id",params.get("gPrdNo"));

//        查询促销条件表的促销条件编号conNo和促销条件名称conName
        List<Map<String,Object>> conList = slgfService.findSaleCdnInfo();
//        调用商品分类的接口 获取 商品分类的编号prdNo 和 商品分分类的名字prdName
        List<Map<String,Object>> map = slgfService.selectPrdInfo();

//        调用商品明细接口 根据 商品分类名称 prdName 查询 商品明细编号prd_dtl_id和名字prd_dtl_name
        List<Map<String,Object>> prdInfo = (List<Map<String, Object>>) ToolUtil.callUrl("http://10.90.86.249:8888/prd/getAll2",param);
        System.out.println("prdInfo详情信息：" + prdInfo);

        params.put("prdInfo",prdInfo);

        System.out.println("params = " + params);


        modelAndView.addObject("params",params);
        modelAndView.addObject("conList",conList);
        modelAndView.addObject("map",map);
        modelAndView.setViewName("slgfDetail");
        return modelAndView;
    }

    /**
     * @description 修改买赠信息
     * @author Lonely
     * @date 17/9/26
     * @return
     * @param
     */
    @RequestMapping("updateSLGFInfo")
    @ResponseBody
    public Map<String,Object> updateSLGFInfo(@RequestParam Map<String,Object> params) {
        Map<String,Object> result = new HashMap<>();
        result.put("updateSLGFInfo",slgfService.updateSLGFInfo(params));
        result.put("result","success");
        return result;
    }


}
